package com.test.restaurant.admin.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.aliyun.oss.*;
import com.aliyun.oss.model.CompleteMultipartUploadResult;
import com.aliyun.oss.model.UploadFileRequest;
import com.aliyun.oss.model.UploadFileResult;
import com.test.restaurant.pojo.Dish;
import com.test.restaurant.service.DishService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Date;
import java.util.List;

/**
 * @author 徒有琴
 */
@Controller
public class DishController {
    @Reference
    private DishService dishService;
    @RequestMapping("/init-solr.html")
    public String solr(){
        dishService.initSolr();
        return "/foodList";
    }

    @RequestMapping("search.html")
    public String search(String key, Model model){
        List<Dish> dishes=dishService.search(key);
        model.addAttribute("dishes",dishes);
        return "/dishes";
    }
    @RequestMapping("upload.html")
    public String upload(@RequestParam("file") MultipartFile file, Integer price) {
        System.out.println(price);
        // Endpoint以杭州为例，其它Region请按实际情况填写。
        try {
            String endpoint = "oss-cn-beijing.aliyuncs.com";
            String accessKeyId = "LTAI4FwDM4Ju6vq9NdQHxLE3";
            String accessKeySecret = "cU9y7TMH97iUfORpywt58bFuvfIE3B";
            // 创建OSSClient实例。
            OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
            // 上传文件流。
            // File file = new File("d:/upload/timg.jfif");
            InputStream inputStream = file.getInputStream();
            long time = System.currentTimeMillis();
            String extName = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
            ossClient.putObject("tyqimg", time + extName, inputStream);
            // 关闭OSSClient。
            ossClient.shutdown();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "upload";
    }

    public static void main(String[] args) {
        //地址在OSS控制台获取。
        String endpoint = "oss-cn-beijing.aliyuncs.com";
        String accessKeyId = "LTAI4FwDM4Ju6vq9NdQHxLE3";
        String accessKeySecret = "cU9y7TMH97iUfORpywt58bFuvfIE3B";
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
// Endpoint以杭州为例，其它Region请按实际情况填写。
        String bucketName = "tyqimg";
        String objectName = "1574232289813.png";

// 设置URL过期时间为1小时。
        Date expiration = new Date(new Date().getTime() + 3600 * 1000);
// 生成以GET方法访问的签名URL，访客可以直接通过浏览器访问相关内容。
        URL url = ossClient.generatePresignedUrl(bucketName, objectName, expiration);
        System.out.println(url);
        System.out.println(url.getQuery());
// 关闭OSSClient。
        ossClient.shutdown();
//        try {
//            UploadFileRequest uploadFileRequest = new UploadFileRequest("tyqimg", "1901.png");
//            // The local file to upload---it must exist.
//            uploadFileRequest.setUploadFile("D:/upload/1566804347150.png");
//            // Sets the concurrent upload task number to 5.
//            uploadFileRequest.setTaskNum(5);
//            // Sets the part size to 1MB.
//            uploadFileRequest.setPartSize(1024 * 1024 * 1);
//            uploadFileRequest.setEnableCheckpoint(true);
//            UploadFileResult uploadResult = ossClient.uploadFile(uploadFileRequest);
//
//            CompleteMultipartUploadResult multipartUploadResult =
//                    uploadResult.getMultipartUploadResult();
//            System.out.println(multipartUploadResult.getLocation());//访问地址
//        } catch (OSSException oe) {
//            System.out.println("Caught an OSSException, which means your request made it to OSS, "
//                    + "but was rejected with an error response for some reason.");
//        } catch (ClientException ce) {
//            System.out.println("Caught an ClientException, which means the client encountered "
//                    + "a serious internal problem while trying to communicate with OSS, "
//                    + "such as not being able to access the network.");
//            System.out.println("Error Message: " + ce.getMessage());
//        } catch (Throwable e) {
//            e.printStackTrace();
//        } finally {
//            ossClient.shutdown();
//        }

    }
}
